package org.healer.simple_juc;

public class UnsafeBuyTicket {
    public static void main(String[] args) {
        BuyTicket bt1=new BuyTicket();
        BuyTicket bt2=new BuyTicket();
        BuyTicket bt3=new BuyTicket();

        new Thread(bt1,"牛逼的你").start();
        new Thread(bt1,"苦逼的我").start();
        new Thread(bt1,"黄牛党").start();


    }
}

class BuyTicket implements Runnable{
    //票
    private int ticketNums = 10;
    boolean flag = true;//外部停止方式

    @Override
    public void run() {
        while (flag){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            buy();
        }
    }
    private synchronized void buy(){
        //判断是否有票
        if(ticketNums<=0){
            flag=false;
            return;
        }
        //买票
        System.out.println(Thread.currentThread().getName()+"拿到"+ticketNums--);
    }
}
